package aaaBrowser;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import aaBaolilo.ImageDemo;
import aaBaolilo.LoginAndPub;
import aaBaolilo.OcrX;
import aaaBrowser.model.RegModel;
import aaaCfg.IocX;

import com.attilax.Closure;
import com.attilax.core;
import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.attilax.lang.ClipboardUtil;
import com.google.inject.Inject;

/**
 * public static String USERNAME = "use2015"; //UU u public static String
 * PASSWORD = "000000";
 * 
 * http://dt4gbk.weijicn.cn/  http://dt4utf.weijicn.cn/    http://dt5utf.weijicn.cn/     http://dt5gbk.weijicn.cn/
 * 
 * @author Administrator
 *
 */
public class Login_finish_info {

	public Login_finish_info() {
		//this.capt = new CaptX();
	}

	public static void main(String[] args) {

		Login_finish_info lfix=IocX.getBean(Login_finish_info.class);
		RegModel mod=new RegModel();
		mod.setCrloginurl("http://dt4utf.weijicn.cn/member/login.php");
		mod.setCrdetailurl("http://dt4utf.weijicn.cn/member/edit.php?tab=2");
		mod.setUsername("attilax");
		mod.setPassword("000000");
		mod.setBusiness("经营范围 经营范围 经营范围 经营范围 ");
		mod.setRegyear("2015");
		mod.setAddress("地址地址地址地址地址地址地址地址");
		mod.setContent("内容内容内容内容内容内容内容内容内容内容内容内容");
		mod.setCapital("captial.........................");
		//mod.set
		lfix.start(mod);
		System.out.println("--main exec finish");
		//new Login_finish_info().start();
	}

	final Display display = new Display();
	atiBrowserExample atiBrowserExampleinstance;

	public void start(final RegModel regModel) {
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		shell.setText("Browser example");
		// shell.setVisible(false);
		// shell.setSize(1, 1);
		atiBrowserExampleinstance = new atiBrowserExample(shell);

		shell.open(); // shell.setVisible(false);
	 	login(regModel);
	 //	finish_info(regModel);  //just 4 test
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
			{
				display.sleep();
			}
		}
		// icon.dispose();
		atiBrowserExampleinstance.dispose();
		display.dispose();
		
	}

	public void login(final RegModel regModel){
	core.execMeth_Ays(new Runnable() {

			@Override
			public void run() {
				System.out.println("=-ajsy");
		Display.getDefault().asyncExec(new Runnable() {

				@Override
				public void run() {
					try {
						
					} catch (Exception e) {
						// TODO: handle exception
					}
					String url = regModel.getCrloginurl().replace("register.php", "login.php").toString();
					atiBrowserExampleinstance.setUrlgoto(url);
					atiBrowserExampleinstance.addProgressListener4exeJStxt(loginJs(regModel), new Closure() {

								@Override
								public Object execute(Object arg0)
										throws Exception {
			core.execMeth_Ays(new Runnable() {

											@Override
											public void run() {
												try {
													Thread.sleep(3000);
												} catch (InterruptedException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
												}
											 	finish_info(regModel);

											}
										}, "");
									return null;
								}

							});

					atiBrowserExampleinstance.regJsCallbackMeth(
							"getCaptch", new Closure() {

								@Override
								public Object execute(Object arg0)
										throws Exception {
									//finish_info(regModel);
											setCaptch();
									return null;
								}
							});

					atiBrowserExampleinstance.gotoUrl();
					
				}
				
			});
			}
		}, "");
	}

	
	public String loginJs(final RegModel regModel){
		String js = filex.read(pathx.classPath(Reg.class) + "\\login.js");
	//	try{
			
			//System.out.println("userName====="+regModel.getUsername()+"pwd======"+regModel.getPassword());
			js = js.replace("@username",regModel.getUsername()).replace("@password",regModel.getPassword());
			return js;
//		}catch(Exception e){
//			e.printStackTrace();
//		}
		//return null;
	}
	
	//没有加载js
	/***************
	 * 	atiBrowserExampleinstance.addProgressListener
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 */
	public void finish_info(final RegModel regModel) {
		
		System.out.println("---finish_info start");
	execMeth_AysGui(new Runnable() {

			@Override
			public void run() {
				try {
					System.out.println(regModel.getCracturl().replace("register.php", "edit.php"));
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				String url = regModel.getCrdetailurl().replace("register.php", "edit.php").toString();
				atiBrowserExampleinstance.setUrlgoto(url);
				atiBrowserExampleinstance.addProgressListener4exeJStxt(
						setFinishInfo(regModel), new Closure() {

							@Override
							public Object execute(Object arg0) throws Exception {
								// pub();

								return null;
							}

						});
				atiBrowserExampleinstance.regJsCallbackMeth(
						"getCaptch", new Closure() {

							@Override
							public Object execute(Object arg0)
									throws Exception {
								//finish_info(regModel);
								return null;
							}
						});

				atiBrowserExampleinstance.gotoUrl();
			}
		},""
	);
		
		

	}

	public String setFinishInfo(final RegModel regModel){
		try{
			String js = filex.read(pathx.classPath(Login_finish_info.class)+"\\finish_info.js");
			js = js.replace("@business", regModel.getBusiness()).replace("@regyear",regModel.getRegyear()).replace("@capital",regModel.getCapital())
			.replace("@address",regModel.getAddress()).replace("@content",regModel.getContent());
			return js;
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
 

@Inject
	CaptX capt;

	public void setCaptch() {

		String captchCode = capt.getTxt();

		String js = filex.read(pathx.classPath(Login_finish_info.class)
				+ "\\set_captch.js");
		js = js.replaceAll("@capt", captchCode);
		// "1234";
		System.out.println("exe js:" + js);
		atiBrowserExampleinstance.browser.execute(js);

	}

	private void execMeth_AysGui(final Runnable runnable, String string) {
		core.execMeth_Ays(new Runnable() {

			@SuppressWarnings("static-access")
			@Override
			public void run() {
				System.out.println("=-ajsy");
				// Display default1 = display.getDefault();
				// runnable.run();
				Display.getDefault().asyncExec(runnable);
				// display.asyncExec(runnable);
			}
		}, string);

	}

}
